<?php

namespace App\Logging;

use Monolog\Formatter\LineFormatter as BaseLineFormatter;

class LineFormatter extends BaseLineFormatter
{
    const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name% %extra% : %message% %context%\n";

    public function format(array $record)
    {
        $arr = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
        $length = count($arr);
        $deep = 7;
        if ($length < 8) {
            // swoole 线程
            $record['extra'][] = $arr[$length - 1]['file'] . ':' . $arr[$length - 1]['function'] . '():' . $arr[$length - 1]['line'];
            return parent::format($record);
        } elseif (!isset($arr[7]['file']) || strpos($arr[7]['file'], '/src/vendor/') > 0 || $length == 8) {
            // console 模式下，少一层
            $deep = 6;
        }
        $arr = array_slice($arr, $deep);
        $record['extra'][] = $arr[0]['file'] . ':' . $arr[1]['function'] . '():' . $arr[0]['line'];
        return parent::format($record);
    }
}
